package com.aa;



import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ContactDetail extends Activity {

	private String TAG = "ContactDetail";
	private TextView mDisplayName;
	private Button mbtnTitleBack;
	private Button mbtnTitlebEdit;
	private LinearLayout mDELButton;
	private LinearLayout mSMSButton;
	private LinearLayout mLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_detail);
		initView();
		initListener();
		TelephonyManager _TelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		Common.iNativePhoneNumber = _TelephonyManager.getLine1Number();
	
		
	}
	private void initView(){
		mDisplayName = (TextView)findViewById(R.id.contact_detail_dispname);
		mbtnTitleBack = (Button)findViewById(R.id.title_btn1);
		mbtnTitlebEdit = (Button)findViewById(R.id.title_btn4);
		mDELButton = (LinearLayout)findViewById(R.id.contact_detail_del_button);
		mSMSButton = (LinearLayout)findViewById(R.id.contact_detail_sms_button);
		mLayout = (LinearLayout)findViewById(R.id.contact_detail_layout);
		
		mbtnTitleBack.setVisibility(View.VISIBLE);
		mbtnTitleBack.setText(R.string.app_back_tip);
		
		mbtnTitlebEdit.setVisibility(View.VISIBLE);
		mbtnTitlebEdit.setText(R.string.contact_detail_edit);
		
		View _View = LayoutInflater.from(this).inflate(R.layout.contact_detail_number_view, null);
	    ((TextView)_View.findViewById(R.id.contact_detail_number_type)).setText(getString(R.string.phone_type_mobile));
	    ((TextView)_View.findViewById(R.id.contact_detail_number_no)).setText(Common.iCallNumber);
	    _View.findViewById(R.id.contact_detail_num_call_layout).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				submitCall();
			}
		});
	    mLayout.addView(_View);
	    
	    mDisplayName.setText(Common.iCallName);
	}
	
	private void initListener(){
		mbtnTitleBack.setOnClickListener(new OnAllClickListner());
		mbtnTitlebEdit.setOnClickListener(new OnAllClickListner());
		mDELButton.setOnClickListener(new OnAllClickListner());
	}

	private class OnAllClickListner implements OnClickListener{

		@Override
		public void onClick(View v) {
			int _ID = v.getId();
			switch (_ID) {
			case R.id.title_btn1:
				finish();
				break;
			case R.id.title_btn4:
				 ContentResolver _ContentResolver = getContentResolver();
			     Uri _Uri = ContactsContract.Contacts.CONTENT_URI;
			     String[] arrayOfString = new String[1];
			     arrayOfString[0] = Common.iCallName;
			     Cursor _Cursor = _ContentResolver.query(_Uri, null, "display_name=?", arrayOfString, null);
			     if (_Cursor.moveToFirst())
			     {
			         String str = _Cursor.getString(_Cursor.getColumnIndex("lookup"));
			         Intent _Intent = new Intent("android.intent.action.EDIT", ContactsContract.Contacts.getLookupUri(_Cursor.getLong(_Cursor.getColumnIndex("_id")), str));
			         _Intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
			         _Intent.putExtra("addToDefaultDirectory", "");
			          ContactDetail.this.startActivity(_Intent);
			          Common.deleteContactFlag = true;
			          Common.deleteCalllogFlag = true;
			       finish();
			     }
			     break;
			case R.id.contact_detail_sms_button:
				
				break;
			case R.id.contact_detail_del_button:
				Delete();
				break;
			case R.id.contact_detail_num_call_layout:
				submitCall();
			default:
				break;
			}
			
		}
		
	}
	

	 public void submitCall(){
		 Log.i(TAG, "iNativePhoneNumber = "+ Common.iNativePhoneNumber);
		 Log.i(TAG, "iCallNumber = "+ Common.iCallNumber);
		Common.InitCallSetData(this, 0);
//		if (Common.isSystemPhoneCall(Common.iCallNumber))
//		{
//		    Intent _Intent = new Intent();
//		    _Intent.setAction("android.intent.action.CALL");
//		    _Intent.setData(Uri.parse("tel:" + Common.iCallNumber));
//		    startActivity(_Intent);
//		    return;
//		 }
//		
		Intent _Intent = new Intent();
		_Intent.setClass(this, CallWaitActivity.class);
		startActivity(_Intent);
	 }
	 
	
	private void Delete(){
		View _View = LayoutInflater.from(this).inflate(R.layout.dialog_title, null);
		new AlertDialog.Builder(this)
		.setCustomTitle(_View)
		.setMessage(R.string.contact_detail_dialog_del)
		.setNegativeButton(R.string.app_cancel, null)
		.setPositiveButton(R.string.app_ok, new OnDeleteClickListener())
		.show();	
	}
	
	private class OnDeleteClickListener implements  android.content.DialogInterface.OnClickListener {
		@Override
		public void onClick(DialogInterface dialog, int which) {
		 int _ContactID = -1;
		 ContentResolver _ContentResolver = getContentResolver();
		 Uri _Uri = ContactsContract.Contacts.CONTENT_URI;
		 String[] _id = { "_id" };
         String[] _name = new String[1];
         _name[0] = Common.iCallName;
         Cursor _Cursor = _ContentResolver.query(_Uri, _id, "display_name=?", _name, null);
         if (_Cursor.moveToFirst())
        	 _ContactID = _Cursor.getInt(_Cursor.getColumnIndex("_id"));
         Uri _Uri2 = ContactsContract.RawContacts.CONTENT_URI;
         String[] _StrID = new String[1];
         _StrID[0] = String.valueOf(_ContactID);
         if (_ContentResolver.delete(_Uri2, "_id=?", _StrID) == 0)
         {
           Toast.makeText(ContactDetail.this, R.string.contact_detail_del_failed, 3000).show();
           return;
         }
         Common.deleteContactFlag = true;
         Common.deleteCalllogFlag = true;
         finish();
		}
		
	}

	
}
